#include "InputHandler.h"
#include "GameSystemManager.h"
#include "GameObjectManager.h"

//bool shutDown;
//bool InputHandler::mouseLeftButton = false;

InputHandler::InputHandler(unsigned long hWnd)  {

	OIS::ParamList pl;
	pl.insert(OIS::ParamList::value_type("WINDOW", Ogre::StringConverter::toString(hWnd)));
		
	/*
	pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND" )));
	pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
	pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_FOREGROUND")));
	pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_NONEXCLUSIVE")));
	*/

	m_hWnd = hWnd;
	mInputManager = OIS::InputManager::createInputSystem( pl );
	mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, true ));
	mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, true));
	mMouse->setEventCallback(this);
	mKeyboard->setEventCallback(this);
	forward=backward=up=down=left=right = false;
	shutDown = false;
}

InputHandler::~InputHandler() {
	if( mInputManager )
	{
		mInputManager->destroyInputObject( mMouse );
		mInputManager->destroyInputObject( mKeyboard );
		//mInputManager->destroyInputObject( mJoy );

		OIS::InputManager::destroyInputSystem(mInputManager);
		mInputManager = 0;
	}
}

void InputHandler::capture() {
	mMouse->capture();
	mKeyboard->capture();
	//mKeyboard->get
	//if(mKeyboard->isKeyDown(OIS::kc_
}

const OIS::MouseState InputHandler::getMouseStat() {
	const OIS::MouseState ms = mMouse->getMouseState();
	return ms;
}

bool InputHandler::isRightButtonDown() {
	const OIS::MouseState ms = mMouse->getMouseState();
	if(ms.buttonDown(OIS::MB_Right))
		return true;
	return false;
}
bool InputHandler::isLeftButtonDown() {
	//mMouse->capture();
	//const OIS::MouseState ms = mMouse->getMouseState();
	const OIS::MouseState &ms = mMouse->getMouseState();
	if(ms.buttonDown(OIS::MB_Left))
		return true;
	return false;
}
bool InputHandler::isMiddleButtonDown() {
	const OIS::MouseState ms = mMouse->getMouseState();
	if(ms.buttonDown(OIS::MB_Middle))
		return true;
	return false;
}

void  InputHandler::setWindowExtents(int width, int height){
	//Set Mouse Region.. if window resizes, we should alter this to reflect as well
	const OIS::MouseState &ms = mMouse->getMouseState();
	ms.width = width;
	ms.height = height;
}


// MouseListener
bool InputHandler::mouseMoved(const OIS::MouseEvent &evt) {
	GameSystemManager::getSingleton()->getGUI()->updateMouseMoved(evt.state.X.rel,evt.state.Y.rel);
	
	//GameSystemManager::getSingleton()->getInput()->Yaw += Radian(-evt.state.X.rel / 2000.0f);
	//GameSystemManager::getSingleton()->getInput()->Pitch += Radian(-evt.state.Y.rel / 2000.0f);

	//GameSystemManager::getSingleton()->getInput()->
	Yaw = Degree(-evt.state.X.rel * 0.3); //* time * 40
	//GameSystemManager::getSingleton()->getInput()->
	Pitch = Degree(-evt.state.Y.rel * 0.3); //* time * 40
	
	//evt.state.X.abs/float(evt.state.width)
	//evt.state.Y.abs/float(evt.state.height)
	
	GameSystemManager::getSingleton()->getTerrain()->updateBrush(evt);
	//static_cast<TerrainSystem*>(GameSystemManager::getSingleton()->getSystem("TerrainSystem"))->updateBrush(evt);
		
	//	->getTerrain()->updateBrush(evt);
	//GameObjectManager::getSingleton()->updateObjectDecal();
	
	//GameObjectManager::getSingleton()->updateObjectRotation();

	return true;
}

bool InputHandler::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID btn) {
	switch(btn) {
		case OIS::MB_Left:
			mouseLeftButton = true;
			break;
		case OIS::MB_Right:
			mouseRightButton = true;
			break;
		case OIS::MB_Middle:
			mouseMiddleButton = true;
			break;
	}
	
	GameSystemManager::getSingleton()->getGUI()->updateMouseButtonDown(btn);
	return true;
}

bool InputHandler::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID btn) {
	switch(btn) {
		case OIS::MB_Left:
			mouseLeftButton = false;
			break;
		case OIS::MB_Right:
			mouseRightButton = false;
			break;
		case OIS::MB_Middle:
			mouseMiddleButton = false;
			break;
	}
	GameSystemManager::getSingleton()->getGUI()->updateMouseButtonUp(btn);
	return true;
}

// KeyListener
bool InputHandler::keyPressed(const OIS::KeyEvent &evt) {
	if(evt.key == OIS::KC_Q)
		up = true;
	if(evt.key == OIS::KC_E)
		down = true;
	if(evt.key == OIS::KC_W)
		forward = true;
	if(evt.key == OIS::KC_S)
		backward = true;
	if(evt.key == OIS::KC_A)
		left = true;
	if(evt.key == OIS::KC_D)
		right = true;
	if(evt.key == OIS::KC_ESCAPE)
		esc = true;

	GameSystemManager::getSingleton()->getGUI()->updateKeyboardKeyPressed(evt);

	return true;
}

bool InputHandler::keyReleased(const OIS::KeyEvent &evt) {
	if(evt.key == OIS::KC_Q)
		up = false;
	if(evt.key == OIS::KC_E)
		down = false;
	if(evt.key == OIS::KC_W)
		forward = false;
	if(evt.key == OIS::KC_S)
		backward = false;
	if(evt.key == OIS::KC_A)
		left = false;
	if(evt.key == OIS::KC_D)
		right = false;

	if(evt.key == OIS::KC_ESCAPE)
		esc = true;

	GameSystemManager::getSingleton()->getGUI()->updateKeyboardKeyReleased(evt);
	return true;
}



// JoyStickListener
bool InputHandler::buttonPressed(const OIS::JoyStickEvent &evt, int index) {
	return true;
}

bool InputHandler::buttonReleased(const OIS::JoyStickEvent &evt, int index) {
	return true;
}

bool InputHandler::axisMoved(const OIS::JoyStickEvent &evt, int index) {
	return true;
}

bool InputHandler::povMoved(const OIS::JoyStickEvent &evt, int index) {
	return true;
}

void InputHandler::updateMouseMetrics(int width,int height) {
	const OIS::MouseState &ms = mMouse->getMouseState();
	ms.width = width;
	ms.height = height;	
}


